package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};

import com.yuan.yblog.common.utils.StringUtils;
import com.yuan.yblog.common.service.EntityCacheService;
import com.yuan.yblog.common.exception.ApiException;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
* 服务实现类
* @author hepeiyuan
*/
@Service
public class ${table.serviceImplName} extends ServiceImpl<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    @Autowired
    EntityCacheService entityCacheService;

    @Autowired
    private ${table.mapperName} ${table.entityPath}Mapper;

    @Override
    public ${entity} selectByIdAndBuildCache(long id) {
        return entityCacheService.selectById(
            ${entity}.class,
            id,
            ${entity?upper_case},
            ${entity?upper_case}_EXPIRE,
            LOCK_${entity?upper_case},
            LOCK_${entity?upper_case}_WAIT,
            ${table.entityPath}Mapper
        );
    }

    @Override
    public boolean insert(${entity} insertEntity) {
        insertEntity.setId(null);
        save(insertEntity);
        return insertEntity.getId() != null;
    }

    @Override
    public boolean updateAndDeleteCache(${entity} updateEntity) {
        if (updateEntity.getId() == null) {
            return false;
        }
        boolean succeed = updateById(updateEntity);
        if (succeed) {
            // 数据发生变化，删除缓存
            entityCacheService.deleteEntity(${entity?upper_case}, updateEntity.getId());
            return true;
        }
        return false;
    }

    @Override
    public boolean updateAndDeleteCache(long id, UpdateWrapper<${entity}> updateWrapper) {
        boolean succeed = update(updateWrapper);
        if (succeed) {
            // 数据发生变化，删除缓存
            entityCacheService.deleteEntity(${entity?upper_case}, id);
            return true;
        }
        return false;
    }

    @Override
    public boolean logicalDeleteAndDeleteCache(long id, String logicalFiled) {
        String filed = logicalFiled;
        if (StringUtils.isEmpty(filed)) {
            filed = "status";
        }
        int count = ${table.entityPath}Mapper.existsColumn(filed);
        if (count <= 0) {
            throw new ApiException("逻辑字段位（" + filed + "）不存在，无法逻辑删除。");
        }
        boolean succeed = update(new UpdateWrapper<${entity}>().set(filed, 0));
        if (succeed) {
            // 数据发生变化，删除缓存
            entityCacheService.deleteEntity(${entity?upper_case}, id);
            return true;
        }
        return false;
    }

    @Override
    public boolean physicalDeleteAndDeleteCache(long id) {
        boolean succeed = removeById(id);
        if (succeed) {
            entityCacheService.deleteEntity(${entity?upper_case}, id);
            return true;
        }
        return false;
    }

}
